<template>
  <div class="app-container">
    <div v-show="type == 0">
      <el-row>
<!--        <el-col :span="4" style="margin-left: -20px;">-->
<!--          <MainType :typeCode="typeCode" @getNode="getNode"></MainType>-->
<!--        </el-col>-->
      <el-col :span="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="项目名称" prop="projectName">
          <el-input
            v-model="queryParams.projectName"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="开始时间"  prop="startTime">
        <el-date-picker clearable style="width:170px"
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable style="width:170px"
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  size="mini" height="450" :row-style="{ height: '30px' }" border :cell-style="{ padding: '0px' }" v-loading="loading" :data="projectInfoList" row-key="id"
    :tree-props="{children: 'children'}"
    efault-expand-all >
      <el-table-column label="序号" type="index" align="center" prop="index" :index="indexMethod"></el-table-column>
      <el-table-column label="项目编号" align="center" prop="projectNumber" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="项目名称" align="center" width="150" prop="projectName" :show-overflow-tooltip="true" >
         <template slot-scope="scope">
           <!-- 使用按钮作为可点击元素，并绑定点击事件 -->
           <el-button size="small" @click="handleClick(scope.row, scope.$index + 1)" type="text">
             {{ scope.row.projectName }}
           </el-button>
         </template>
      </el-table-column>
      <el-table-column label="项目负责人" align="center" prop="projectLeader" width="130" :show-overflow-tooltip="true"/>
      <el-table-column label="计划开始时间" align="center" prop="startTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划结束时间" align="center" prop="endTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目执行人" align="center" prop="executorName" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_status" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="creationName" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="creationTime" width="90">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="70" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.executorId==userId||scope.row.projectLeaderId==userId">
          <el-button v-if="scope.row.sign=='主'"
            size="mini"
            type="text"
            icon="el-icon-copy-document"
            @click="handleJF(scope.row,scope.$index + 1)"
                     title="交付项目"
          ></el-button>
          <el-button v-if="scope.row.sign=='主'"
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleOK(scope.row)"
                     title="完成项目"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-col>
      </el-row>
  </div>
    <!-- 项目交付 -->
    <div v-if="type == 1">
      <Operate @change="close" :opt="edit" :id="id" :sign="sign" :index="index" ></Operate>
    </div>

    <!-- 客服评价 -->
    <!-- <div v-if="type == 2">
      <Evaluate @change="close" :opt="edit" :id="id" :sign="sign" :index="index" ></Evaluate>
    </div> -->

  </div>
</template>

<script>
import { listProjectInfo,listProjectTree, getProjectInfo, delProjectInfo, addProjectInfo,editMainClass, updateProjectInfo } from "@/api/project/projectInfo";
import {getScheduleName} from "@/api/project/delivery";
import MainType from "@/components/MainType/indexNew";
import { getToken } from "../../../utils/auth";
import Operate from "./operate.vue";

import { listType } from "@/api/maintype/type";
import Treeselect from "@riophae/vue-treeselect";
import { queryFile, fileRemove } from "@/api/system/file";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { MainType, Operate,Treeselect },
  dicts: ['project_status'],
  name: "projectSchedule",
  data() {
    return {
      userId:"",
      typeCode:'project',
      type: 0,
      opt: 'add',
      id: '',
      sign:'',
      obj: null,
      index: null,
      edit: 'edit',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目信息表格数据
      projectInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        state: null,
        projectName: null,
        startTime: null,
        endTime: null,
        attackTime: null,
        completeTime: null,
      },
      queryParamsZ: {
        type: null,

      },
      queryParamsSchedule: {
        projectId: null,
        state:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.userId = this.$store.state.user.info.userId
  },
  methods: {
    //项目名称跳转
    handleClick(row,index) {
      this.reset();
      this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index
      this.edit = "details"
      this.id = row.id;
      this.sign = row.sign;
      this.type = 1
    },
    indexMethod(index) {
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
    },
    //树点击事件
    getNode(data, node, obj) {
      this.queryParams.classificationId = data.id
      this.getList()
    },
    /** 查询项目信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.state = 3//只查询交付状态的项目信息
      listProjectTree(this.queryParams,this.queryParamsZ).then(response => {
        this.projectInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    close(data) {
      this.type = 0
      this.getList();
      this.open=data;

    },
     /** 交付按钮操作 */
     handleJF(row, index) {
      this.reset();
      this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index
      this.edit = "edit"
      this.id = row.id;
      this.sign = row.sign;
      this.type = 1
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        classificationId: null,
        projectNumber: null,
        projectName: null,
        contractId: null,
        contractName: null,
        projectLeaderId: null,
        projectLeader: null,
        executorId: null,
        executorName: null,
        startTime: null,
        endTime: null,
        attackTime: null,
        completeTime: null,
        state: null,
        operateTime: null,
        operateId: null,
        operateName: null,
        departmentId: null,
        departmentName: null,
        organizationId: null,
        organizationName: null,
        creationId: null,
        creationName: null,
        creationTime: null,
        belongTo: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const projectId = row.projectId || this.ids
      getProjectInfo(projectId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目信息";
      });
    },
    /** 完成按钮操作 */
    handleOK(row) {
      //需要判断是否有上传交付物
      this.businessId = row.id
      this.businessCode = "project_delivery"
      //获取文件信息
      queryFile(this.businessId, this.businessCode).then(response => {
        if(response.data.length==0){
          this.$message.error('请先上传交付物！');
          return;
        }else{
          this.form.projectId = row.id
          this.form.state = 6
          this.$modal.confirm('是否确认完成交付项目名称为"' + row.projectName + '"的数据项？').then(function() {
          }).then(() => {
            updateProjectInfo(this.form).then(response1 => {
              this.getList();
              this.$modal.msgSuccess("操作成功");
          });
          }).catch(() => {});
        }
      });
    },
  }
};
</script>
